<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>管理系统</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
</head>
<body>
<div id="a">
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
        <el-row>
            <el-col :span="12">
                <el-form-item label="老师名称" prop="name">
                    <el-input v-model="formData.name"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="老师年龄" prop="age">
                    <el-input v-model="formData.age"/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-form-item
                v-for="(domain, index) in dynamicValidateForm.domains"
                :label="'学生' + index"
                :key="domain.sname"
                :prop="domain.sid"
        >
            <el-table-column v-model="domain.value"></el-table-column><el-button @click.prevent="removeDomain(domain)">删除</el-button>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
            <el-button @click="addDomain">新增学生</el-button>
            <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
        </el-form-item>
    </el-form>
</div>
</body>
<script>
    new Vue({
            el:"#a",
            data:{
                dynamicValidateForm: {
                     domains: [{value: ''}],
                },
                formData: {},//表单数据

            },
            methods: {
                submitForm(formName) {
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            axios.post("/student/add?ids="+this.ids,this.formData).then((res) => {
                                this.$message({
                                    type:res.data.flag?'success':'error',
                                    message:res.data.message
                                });
                            })
                            alert('submit!');
                        } else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },
                resetForm(formName) {
                    this.$refs[formName].resetFields();
                },
                removeDomain(item) {
                    var index = this.dynamicValidateForm.domains.indexOf(item)
                    if (index !== -1) {
                        this.dynamicValidateForm.domains.splice(index, 1)
                    }
                },
                addDomain() {
                    axios.get("/student/find").then(res=>{
                        this.dynamicValidateForm.domains.push({
                            value: res.data.sname,
                            key: res.da
                        });
                        this.dynamicValidateForm.domains = res.data;
                    })

                }
            }
    })
</script>
</html>